/**
 * This file is part of the CAKIS project.
 * 
 * Copyright (c) 2009-2010 - Florian Arndt, Christian Kerl
 * 
 * @license GNU Lesser General Public License v3 <http://www.opensource.org/licenses/lgpl-3.0.html>
 * @since   01.03.2010
 * @version SVN: $Id: RecipeList.java 146 2010-05-15 12:16:33Z christian-kerl@web.de $
 */

package org.cakis.server.action.impl;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.cakis.server.Names;
import org.cakis.server.model.Recipe;
import org.cakis.server.model.repository.RecipeRepository;
import org.cakis.server.service.RecipeService;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.*;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.framework.Controller;
import org.jboss.seam.log.Log;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.ByteMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

@Name("recipeList")
@Scope(ScopeType.CONVERSATION)
@AutoCreate
public class RecipeList extends Controller {
    public static class SelectableRecipeWrapper implements Serializable {
        private static final long serialVersionUID = 1L;

        private boolean selected;
        private Recipe recipe;

        public SelectableRecipeWrapper() {
        }

        public SelectableRecipeWrapper(Recipe recipe) {
            setRecipe(recipe);
        }

        public boolean getSelected() {
            return selected;
        }

        public void setSelected(boolean selected) {
            this.selected = selected;
        }

        public Recipe getRecipe() {
            return recipe;
        }

        public void setRecipe(Recipe recipe) {
            this.recipe = recipe;
        }
    }

    private static final long serialVersionUID = 1L;

    @Logger
    private Log log;

    @In(Names.RecipeRepository)
    private RecipeRepository repository;

    @DataModel("recipes")
    private List<SelectableRecipeWrapper> recipes;

    private List<Recipe> recipesToPrint;

    @Out(required = false)
    public List<Recipe> getRecipesToPrint() {
        return recipesToPrint;
    }

    @Factory("recipes")
    public void initRecipes() {
        List<Recipe> result = repository.findAllOrderByName();

        recipes = new ArrayList<SelectableRecipeWrapper>();

        for(Recipe recipe : result) {
            recipes.add(new SelectableRecipeWrapper(recipe));
        }
    }

    @Begin
    public void print() {
        recipesToPrint = new ArrayList<Recipe>();

        for(SelectableRecipeWrapper wrapper : recipes) {
            if(wrapper.getSelected()) {
                log.info("Selected Recipe #0", wrapper.getRecipe().getName());
                recipesToPrint.add(wrapper.getRecipe());
            }
        }
    }

    public java.awt.Image printQRCode(Recipe recipe) {
        log.info("Generating QRCode for Recipe #0", recipe.getName());

        QRCodeWriter writer = new QRCodeWriter();
        ByteMatrix matrix = null;

        try {
            matrix = writer.encode(RecipeService.Remote.RecipeUuidPrefix + recipe.getUuid(), BarcodeFormat.QR_CODE, 140, 140);
        } catch (WriterException e) {
            log.error("Error generating QRCode for Recipe #0", e, recipe.getName());
        }

        java.awt.Image result;

        if(matrix != null) {
            result = new BufferedImage(matrix.getWidth(), matrix.getHeight(), BufferedImage.TYPE_INT_RGB);

            log.info("Created Image[#0x#1]", matrix.getWidth(), matrix.getHeight());

            Graphics g = result.getGraphics();
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, matrix.getWidth(), matrix.getHeight());
            g.setColor(Color.BLACK);

            for(int x = 0; x < matrix.getWidth(); x++) {
                for(int y = 0; y < matrix.getHeight(); y++) {
                    if(matrix.get(x, y) == 0) {
                        g.fillRect(x, y, 1, 1);
                    }
                }
            }
            result.flush();
        } else {
            result = new BufferedImage(0, 0, BufferedImage.TYPE_INT_RGB);
        }

        log.info("Generated QRCode for Recipe #0", recipe.getName());

        return result;
    }
}
